AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "common/warning.wav" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Pressure_Increment = 3
local Energy_Increment = 5

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.damaged = 0
	LS_RegisterEnt(self.Entity)

	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(100)

	end
end

function ENT:Setup()
	self:TriggerInput("On", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
				if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", self.Active) end
			end
		end
	end
end


function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:Use()
end

function ENT:Output()
	return 1
end

function ENT:Pump_Air()
	energy = RD_GetResourceAmount(self, "energy")
	if (energy >= Energy_Increment) then
		local inc = 0
		if (self.environment.atmosphere > 0) then inc = math.random(1, Pressure_Increment) end


		local hydros = ents.FindByClass( "air_hydro" )
		for _, ent in ipairs( hydros ) do
			if self.Entity:GetPos():Distance(ent.Entity:GetPos()) < ent.range then
				if (ent.product > 0) then
					inc = inc + 1
					ent.product = 0
				end
			end
		end
		if ((self.damaged == 1) and (math.random(1, 10) <= 4)) then inc = 0 end
		if (inc > 0) then
			RD_SupplyResource(self, "air", inc)
		end
		RD_ConsumeResource(self, "energy", Energy_Increment)
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", inc) end
	else
		self.Entity:EmitSound( "common/warning.wav" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
	end
		
	return
end

function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "Oxygen Scrubber (OFF)\nEnergy: " .. self.val1 )
	else
		self:SetOverlayText( "Oxygen Scrubber (ON)\nEnergy: " .. self.val1 )
	end
	if ( self.timer > CurTime() ) then return end
	self.val1 = RD_GetResourceAmount(self, "energy")
	if ( self.Active == 1 ) then
		self:Pump_Air()
	end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Active = 1
		else
			self.Active = 0
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
		end
	end
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
